package com.twh.www.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.twh.www.entity.WnCart;
import com.twh.www.entity.WnGoods;
import com.twh.www.service.WnCartService;
import com.twh.www.mapper.WnCartMapper;
import com.twh.www.service.WnGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class WnCartServiceImpl extends ServiceImpl<WnCartMapper, WnCart>
    implements WnCartService{

    @Autowired
    private WnGoodsService goodsService;

    @Override
    public List<WnCart> getCartByUserId(Integer userId) {
        return this.lambdaQuery().eq(WnCart::getUserId, userId).list();
    }

    @Override
    public String addToCart(Integer userId, Integer goodsId, Integer num) {
        WnGoods goods = goodsService.getById(goodsId);
        if (goods == null) {
            return "fail:notfound";
        }
        if (!"Y".equals(goods.getStatus())) {
            return "fail:off";
        }
        if (goods.getStock() < num) {
            return "fail:stock";
        }
        WnCart cart = this.lambdaQuery().eq(WnCart::getUserId, userId).eq(WnCart::getGoodsId, goodsId).one();
        if (cart != null) {
            cart.setNum(cart.getNum() + num);
            cart.setUpdateTime(LocalDateTime.now());
            return this.updateById(cart) ? "success" : "fail";
        } else {
            cart = new WnCart();
            cart.setUserId(userId);
            cart.setGoodsId(goodsId);
            cart.setGoodsName(goods.getName());
            cart.setGoodsImage(goods.getImage());
            cart.setGoodsPrice(goods.getSalePrice());
            cart.setNum(num);
            cart.setCreateTime(LocalDateTime.now());
            cart.setUpdateTime(LocalDateTime.now());
            return this.save(cart) ? "success" : "fail";
        }
    }

    @Override
    public boolean updateCartNum(Integer cartId, Integer num) {
        WnCart cart = this.getById(cartId);
        if (cart == null) return false;
        WnGoods goods = goodsService.getById(cart.getGoodsId());
        if (goods == null) return false;
        if (goods.getStock() < num) {
            // 库存不足，返回false，Controller中会返回"outofstock"
            return false;
        }
        cart.setNum(num);
        cart.setUpdateTime(LocalDateTime.now());
        return this.updateById(cart);
    }

    @Override
    public boolean removeCartItem(Integer cartId) {
        return this.removeById(cartId);
    }

    @Override
    public BigDecimal getCartTotalPrice(Integer userId) {
        List<WnCart> cartList = getCartByUserId(userId);
        return cartList.stream()
                .map(item -> item.getGoodsPrice().multiply(BigDecimal.valueOf(item.getNum())))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
    }

    @Override
    public void removeByUserId(Integer userId) {
        this.lambdaQuery().eq(WnCart::getUserId, userId).list().forEach(item -> this.removeById(item.getId()));
    }
}




